// Récupération des données
//TODO - mettre dynamique l'id team
var idTeam = window.location.pathname.split('/')[2]
$.ajax({url: 'http://leekwars.chocakai.org/report/list.php?teamId=' + idTeam}).done(function (data) {
	var data = $.parseJSON(data);
 
	// Construction du tableau des combats
	var fights = new Object();
	
	for (var iData in data) {
		if(!fights[data[iData].fightId]) {
			fights[data[iData].fightId] = new Object();
			fights[data[iData].fightId].max = [data[iData].dmg_out, data[iData].heal_out,data[iData].shield_out_abs,data[iData].shield_out_rel];
		} else {
			fights[data[iData].fightId].max[0] += data[iData].dmg_out;
			fights[data[iData].fightId].max[1] += data[iData].heal_out;
			fights[data[iData].fightId].max[2] += data[iData].shield_out_abs;
			fights[data[iData].fightId].max[3] += data[iData].shield_out_rel;
		}
		
		fights[data[iData].fightId][data[iData].leekId] = [data[iData].dmg_out,data[iData].heal_out,data[iData].shield_out_abs,data[iData].shield_out_rel];
	}
	
	// Consolidation des données par poireaux
	var averages = new Object();
	for (var iFight in fights) {
		for(var iLeek in fights[iFight]) {
			if (iLeek != "max") {
				if (!averages[iLeek]) {
					averages[iLeek] = new Object();
					averages[iLeek].me = [0, 0, 0, 0];
					averages[iLeek].max = [0, 0, 0, 0];
					averages[iLeek].count = 0;
				}
				for(var i in averages[iLeek].me) {
					averages[iLeek].me[i] = averages[iLeek].me[i] + fights[iFight][iLeek][i];
					averages[iLeek].max[i] = averages[iLeek].max[i] + fights[iFight]['max'][i];
				}			
				averages[iLeek].count = averages[iLeek].count + 1;
			}
		}
	}
	
	// Calcul des moyennes
	for (var iLeek in averages) {
		for(var i in averages[iLeek].me) {
			/*averages[iLeek].me[i] = averages[iLeek].me[i] / averages[iLeek].count;
			averages[iLeek].max[i] = averages[iLeek].max[i] / averages[iLeek].count;*/
			
			// Mise en pourcentage
			averages[iLeek].me[i] = averages[iLeek].me[i] * 100 / averages[iLeek].max[i];
			averages[iLeek].max[i] = 100;
		}
	}
	
	// Consolidation par composition
	var compositions = new Object();
	
	
	
	console.log("averages");
	console.log(averages);
	
	for (var iLeek in averages) {
		createChartLeek(iLeek, averages[iLeek]);
	}
	
});



//
// Construction du graphe pour un leek
//
function createChartLeek(leek, leekAverages) {
	
	// ajout dans le DOM
	$('a.leek[leek*="' + leek + '"]').each(function() {
		$(this).border = 'solid 1px grey';
		$(this).append("<canvas id=\"stat" + leek + "\" width=\"400\" height=\"400\"></canvas>");
	});
	

	// création des données
	var data = {
		labels: ['Dégâts','Soins','Bouclier abs.','Bouclier rel.'],
		datasets: [
			{
				label: leek,
				fillColor: "rgba(0,130,0,0.8)",
				strokeColor: "rgba(0,130,0,1)",
				pointColor: "rgba(0,130,0,1)",
				pointStrokeColor: "#fff",
				pointHighlightFill: "#fff",
				pointHighlightStroke: "rgba(220,220,220,1)",
				data: leekAverages.me
			},
			{
				label: "max",
				
				fillColor: "rgba(220,220,220,0.2)",
				strokeColor: "rgba(220,220,220,1)",
				pointColor: "rgba(220,220,220,1)",
				pointStrokeColor: "#fff",
				pointHighlightFill: "#fff",
				pointHighlightStroke: "rgba(151,187,205,1)",
				data: leekAverages.max
			}
		]
	};
	
	// lancement du graphes
	var chartCtx = $("#stat" + leek).get(0).getContext("2d");
	var chart = new Chart(chartCtx).Radar(data,getRadarOptions());	
}

function getRadarOptions() {
	var chartOptions = {
		//Boolean - Whether to show lines for each scale point
		scaleShowLine : true,

		//Boolean - Whether we show the angle lines out of the radar
		angleShowLineOut : true,

		//Boolean - Whether to show labels on the scale
		scaleShowLabels : false,

		// Boolean - Whether the scale should begin at zero
		scaleBeginAtZero : true,

		//String - Colour of the angle line
		angleLineColor : "rgba(0,0,0,.1)",

		//Number - Pixel width of the angle line
		angleLineWidth : 1,

		//String - Point label font declaration
		pointLabelFontFamily : "'Arial'",

		//String - Point label font weight
		pointLabelFontStyle : "normal",

		//Number - Point label font size in pixels
		pointLabelFontSize : 10,

		//String - Point label font colour
		pointLabelFontColor : "#666",

		//Boolean - Whether to show a dot for each point
		pointDot : true,

		//Number - Radius of each point dot in pixels
		pointDotRadius : 3,

		//Number - Pixel width of point dot stroke
		pointDotStrokeWidth : 1,

		//Number - amount extra to add to the radius to cater for hit detection outside the drawn point
		pointHitDetectionRadius : 20,

		//Boolean - Whether to show a stroke for datasets
		datasetStroke : true,

		//Number - Pixel width of dataset stroke
		datasetStrokeWidth : 2,

		//Boolean - Whether to fill the dataset with a colour
		datasetFill : true,
	}
	return chartOptions;
}


